package com.utils;

import android.content.Context;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * Created by tanyan on 2018-05-10.
 * 应用配置信息类
 */
public class AppConfig {
    private static final String DEFAULT_PREFERENCES_NAME = "appconfig";
    private static AppConfig instance;
    private Context m_appContext;

    /**
     * 获取单件实例
     *
     * @return
     */
    public static AppConfig getInstance() {
        if (null == instance)
            instance = new AppConfig();
        return instance;
    }

    /**
     * 序列化对象
     *
     * @param k
     * @return String
     */
    public static <K> String serialize(K k) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(
                    byteArrayOutputStream);
            objectOutputStream.writeObject(k);
            String serStr = byteArrayOutputStream.toString("ISO-8859-1");
            serStr = URLEncoder.encode(serStr, "UTF-8");
            objectOutputStream.close();
            byteArrayOutputStream.close();
            return serStr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 反序列化对象
     *
     * @param str
     * @return
     * @throws IOException
     */
    public static <K> K deSerialization(String str) {
        String redStr = null;
        try {
            redStr = URLDecoder.decode(str, "UTF-8");
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(redStr.getBytes("ISO-8859-1"));
            ObjectInputStream objectInputStream = new ObjectInputStream(
                    byteArrayInputStream);
            K person = (K) objectInputStream.readObject();
            objectInputStream.close();
            byteArrayInputStream.close();
            return person;
        } catch (Exception e) {
            return null;
        }
    }

    public void init(Context c) {
        m_appContext = c;
        SharedPrefData.init(m_appContext, DEFAULT_PREFERENCES_NAME);
    }

    /**
     * 获取设备标识
     *
     * @return
     */
    public String getmDeviceId() {
        return SharedPrefData.getString("mDeviceId", "");
    }

    /**
     * 设置设备标识
     *
     * @param mDeviceId
     */
    public void setmDeviceId(String mDeviceId) {
        SharedPrefData.putString("mDeviceId", mDeviceId);
    }

    /**
     * 获取IM登录状态【0】登录成功【1】正在登录【2】登录失败
     *
     * @return
     */
    public int getIMLoginState() {
        return SharedPrefData.getInt("IMLoginState", -1);
    }

    /**
     * 【0】登录成功【1】正在登录【2】登录失败
     *
     * @param IMLoginState
     */
    public void setIMLoginState(int IMLoginState) {
        SharedPrefData.putInt("IMLoginState", IMLoginState);
    }

    /**
     * 获取即时通讯 ID
     *
     * @return
     */
    public String getmImId() {
        return SharedPrefData.getString("ImId", "");
    }

    /**
     * 设置用户即时通讯ID
     *
     * @param m_ImId
     */
    public void setmImId(String m_ImId) {
        SharedPrefData.putString("ImId", m_ImId);
    }

    /**
     * 获取是否是从会话详情及进入
     *
     * @return
     */
    public boolean getmDeatilsInputMessage() {
        return SharedPrefData.getBoolean("DeatilsInputMessage", false);
    }

    /**
     * 设置是否是从会话详情进入
     *
     * @param m_IsDeatilsInputMessage
     */
    public void setmDeatilsInputMessage(boolean m_IsDeatilsInputMessage) {
        SharedPrefData.putBoolean("DeatilsInputMessage", m_IsDeatilsInputMessage);
    }

    /**
     * 获取用户登录类型 1经纪人用户，2置业顾问
     *
     * @return
     */
    public String getUserLoginType() {
        return SharedPrefData.getString("LoginType", "-1");
    }

    /**
     * 设置用户登录类型  1经纪人用户，2置业顾问
     *
     * @param mLoginType
     */
    public void setUserLoginType(String mLoginType) {
        SharedPrefData.putString("LoginType", mLoginType);
    }

    /**
     * 获取是否第一次安装进入
     *
     * @return
     */
    public boolean getIsFirstOpenWelcome() {
        return SharedPrefData.getBoolean("isFirstOpenWelcome", false);
    }

    /**
     * 设置是否第一次安装进入
     *
     * @param isFirstOpen
     */
    public void setIsFirstOpenWelcome(boolean isFirstOpen) {
        SharedPrefData.putBoolean("isFirstOpenWelcome", isFirstOpen);
    }

    /**
     * 获取是否存在崩溃
     *
     * @return
     */
    public boolean getCrashHappen() {
        return SharedPrefData.getBoolean("crashHappen", false);
    }

    /**
     * 设置存在崩溃
     *
     * @param bHappen
     */
    public void setCrashHappen(boolean bHappen) {
        SharedPrefData.putBoolean("crashHappen", bHappen);
    }

    public boolean getKeyState() {
        return SharedPrefData.getBoolean("m_bKey", false);
    }

    public void setKeyState(boolean isKey) {
        SharedPrefData.putBoolean("m_bKey", isKey);
    }

    /**
     * 选择城市id
     */
    public void setCityId(String id) {
        SharedPrefData.putString("CityId", id);
    }

    /**
     * 选择城市id
     */
    public String getCityId() {
        String cityId = SharedPrefData.getString("CityId", "500000");
        return cityId;
    }


    /**
     * 选择城市名称
     */
    public void setCityName(String id) {
        SharedPrefData.putString("CityName", id);
    }

    /**
     * 选择城市名称
     */
    public String getCityName(Context context) {
        String cityId = SharedPrefData.getString("CityName", "重庆");
        return cityId;
    }

//    /**
//     * 获取城市地址
//     *
//     * @return
//     */
//    public String getCityDomainNewest() {
//        CityInfoModel infoModel = ToolUtils.readCityInfo(m_appContext);
//        if (infoModel == null) {
//            infoModel = new CityInfoModel();
//            infoModel.setDomain("/cq");
//        }
//        return infoModel.getDomain();
//    }


    public String getUserHeadUrl() {
        return SharedPrefData.getString("UserHeadUrl", "");
    }

    public void setUserHeadUrl(String UserHeadUrl) {
        SharedPrefData.putString("UserHeadUrl", UserHeadUrl);
    }

    /**
     * 获取图片服务器前缀
     */
    public String getImageURL() {
        return SharedPrefData.getString("PublicImageURL", "");
    }

    /**
     * 保存图片服务器前缀
     */
    public void setImageURL(String string) {
        SharedPrefData.putString("PublicImageURL", string);
    }

    /**
     * 获取图片服务器私有地方前缀
     *
     * @return
     */
    public String getmPrivateImageURL() {
        return SharedPrefData.getString("PrivateImageURL", "");
    }

    /**
     * 保存图片服务器私有地方前缀
     */
    public void setmPrivateImageURL(String mPrivateImageURL) {
        SharedPrefData.putString("PrivateImageURL", mPrivateImageURL);

    }

    /**
     * 获取请求地址
     *
     * @return
     */
    public String getmHttpURL() {
        return SharedPrefData.getString("HttpURL", "");
    }

    /**
     * 设置请求地址
     *
     * @param mHttpURL
     */
    public void setmHttpURL(String mHttpURL) {
        SharedPrefData.putString("HttpURL", mHttpURL);
    }

    /**
     * 是否开启日志输出
     *
     * @return
     */
    public boolean ismDebuggable() {
        return SharedPrefData.getBoolean("mDebuggable", false);
    }

    /**
     * 是否开启日志输出
     *
     * @param mDebuggable
     */
    public void setmDebuggable(boolean mDebuggable) {
        SharedPrefData.putBoolean("mDebuggable", mDebuggable);
    }

    /**
     * 保存全景，视频前缀
     *
     * @param bucketDomain
     */
    public void setmBucketDomain(String bucketDomain) {
        SharedPrefData.putString("BucketDomain", bucketDomain);
    }

    /**
     * 获取全景，视频前缀
     *
     * @return
     */
    public String getmBucketDomain() {
        return SharedPrefData.getString("BucketDomain", "https://daojiale.oss-cn-hangzhou.aliyuncs.com");
    }

    /**
     * 设置房源视频上传路径
     */
    public void setUploadVideoPath(String uploadVideoPath) {
        SharedPrefData.putString("uploadVideoPath", uploadVideoPath);
    }

    /**
     * 获取房源视频上传路径
     */
    public String getUploadVideoPath() {
        return SharedPrefData.getString("uploadVideoPath", "daojiale_test/video/house");
    }


    /**
     * 设置动态视频上传路径
     */
    public void setUploadVideoPathDt(String uploadVideoPathDt) {
        SharedPrefData.putString("uploadVideoPathDt", uploadVideoPathDt);
    }

    /**
     * 获取动态视频上传路径
     */
    public String getUploadVideoPathDt() {
        return SharedPrefData.getString("uploadVideoPathDt", "daojiale_test/video/dynamic");
    }

    /**
     * 获取户关注数量
     */
    public String getAttentionNum() {
        return SharedPrefData.getString("AttentionNum", "");
    }

    /**
     * 保存用户关注数量
     */
    public void setAttentionNum(String string) {
        SharedPrefData.putString("AttentionNum", string);
    }

    /**
     * 获取登录的电话号码
     */
    public String getPhone() {
        return SharedPrefData.getString("phone", "");
    }

    /**
     * 保存登录的电话号码
     */
    public void setPhone(String phone) {
        SharedPrefData.putString("phone", phone);
    }

    /**
     * 根据获取用户ID
     */
    public String getUserId() {
        return SharedPrefData.getString("UserID", "");
    }

    /**
     * 根据用户ID
     *
     * @param userId 用户id号
     */
    public void setUserId(String userId) {
        SharedPrefData.putString("UserID", userId);
    }

    /**
     * 根据获取用户token
     */
    public String getToken() {
        return SharedPrefData.getString("Token", "");

    }

    /**
     * 用户token
     *
     * @param token
     */
    public void setToken(String token) {
        SharedPrefData.putString("Token", token);
    }

    /**
     * 根据获取用户名
     */
    public String getUserName() {
        return SharedPrefData.getString("userName", "");
    }

    /**
     * 根据获取用户昵称
     */
    public String getNickName() {
        return SharedPrefData.getString("NickName", "");
    }

    /**
     * 获取微聊昵称
     */
    public String getImNickName() {
        return SharedPrefData.getString("imNickName", "");
    }

    /**
     * 设置微聊昵称
     */
    public void setImNickName(String nickName) {
        SharedPrefData.putString("imNickName", nickName);
    }

    /**
     * 用户名称
     *
     * @param userName
     */
    public void setUserName(String userName) {
        SharedPrefData.putString("userName", userName);
    }

    /**
     * 用户昵称
     *
     * @param nickName
     */
    public void setNickName(String nickName) {
        SharedPrefData.putString("NickName", nickName);
    }

    /**
     * 是否有版本更新
     *
     * @return
     */
    public boolean isHaveVersionUpdate() {
        return SharedPrefData.getBoolean("isHaveVersionUpdate", false);
    }

    /**
     * 是否有版本更新
     *
     * @param isHaveVersionUpdate
     */
    public void setIsHaveVersionUpdate(boolean isHaveVersionUpdate) {
        SharedPrefData.putBoolean("isHaveVersionUpdate", isHaveVersionUpdate);
    }

    /**
     * 获取登录账号
     *
     * @return
     */
    public String getLoginNumber() {
        return SharedPrefData.getString("loginNumber", "");
    }

    /**
     * 设置储存登录账号
     *
     * @param loginNumber
     */
    public void setLoginNumber(String loginNumber) {
        SharedPrefData.putString("loginNumber", loginNumber);
    }

    /**
     * 获取登录后返回的webtoken
     *
     * @return
     */
    public String getM_webToken() {
        return SharedPrefData.getString("m_webToken", "");
    }

    /**
     * 设置登录后返回的webtoken
     *
     * @param m_webToken
     */
    public void setWebToken(String m_webToken) {
        SharedPrefData.putString("m_webToken", m_webToken);
    }

    /**
     * 设置全局筛选条件
     *
     * @param mFiltrate
     */
    public void setAllFiltrate(String mFiltrate) {
        SharedPrefData.putString("AllFiltrate", mFiltrate);
    }

    public String getmAllFiltrate() {
        return SharedPrefData.getString("AllFiltrate", "");
    }

    /**
     * 获取通过服务器获取的融云Token
     *
     * @return
     */
    public String getRongToken() {
        return SharedPrefData.getString("RongToken", "");
    }

    /**
     * 设置通过服务器获取的融云Token
     *
     * @param rongToken
     */
    public void setRongToken(String rongToken) {
        SharedPrefData.putString("RongToken", rongToken);
    }

    /**
     * 获取直播协议WEB
     *
     * @return
     */
    public String getLiveProtocol() {
        return SharedPrefData.getString("liveProtocol", "");
    }

    /**
     * 设置直播协议WEB
     *
     * @return
     */
    public void setLiveProtocol(String liveProtocol) {
        SharedPrefData.putString("liveProtocol", liveProtocol);

    }

    /**
     * 获取获取到家了币说明WEB
     *
     * @return
     */
    public String getDjlbRule() {
        return SharedPrefData.getString("djlbRule", "");
    }

    /**
     * 设置到家了币说明WEB
     *
     * @return
     */
    public void setDjlbRule(String djlbRule) {
        SharedPrefData.putString("djlbRule", djlbRule);
    }

    /**
     * 获取全景图片上传路径
     *
     * @return
     */
    public String getUploadImagePath() {
        return SharedPrefData.getString("uploadImagePath", "images");
    }

    /**
     * 设置全景图片上传路径
     *
     * @return
     */
    public void setUploadImagePath(String uploadImagePath) {
        SharedPrefData.putString("uploadImagePath", uploadImagePath);
    }

    /**
     * 获取动态图片或动态视频封面上传路径
     *
     * @return
     */
    public String getUploadDynamicPath() {
        return SharedPrefData.getString("uploadDynamicPath", "daojiale_test/images/dynamic");
    }

    /**
     * 设置动态图片或动态视频封面上传路径
     *
     * @return
     */
    public void setUploadDynamicPath(String uploadDynamicPath) {
        SharedPrefData.putString("uploadDynamicPath", uploadDynamicPath);
    }

    /**
     * 获取H5域名
     *
     * @return
     */
    public String getmDomain() {
        return SharedPrefData.getString("mDomain", "");
    }

    /**
     * 设置H5域名
     */
    public void setmDomain(String mDomain) {
        SharedPrefData.putString("mDomain", mDomain);
    }

    /**
     * 获取城市访问地址  如（/cq）
     *
     * @return
     */
    public String getDomain() {
        return SharedPrefData.getString("Domain", "/cq");
    }

    /**
     * 设置城市访问地址  如（/cq）
     */
    public void setDomain(String Domain) {
        SharedPrefData.putString("Domain", Domain);
    }

}
